﻿using Exceptionless;
using System;
using System.Text;

namespace SX.Core.Logging
{
    /// <summary>
    /// Trace 包含大量的信息，例如 protocol payloads。一般仅在开发环境中启用, 仅输出不存文件。
    /// Debug  比 Trance 级别稍微粗略，一般仅在开发环境中启用, 仅输出不存文件。
    /// Info 一般在生产环境中启用。
    /// Warn 一般用于可恢复或临时性错误的非关键问题。
    /// Error 一般是异常信息。
    /// Fatal - 非常严重的错误！
    /// </summary>
    public class LogHelper
    {
        private readonly ExceptionlessClient logger;

        private LogHelper(ExceptionlessClient logger)
        {
            this.logger = logger;
        }
        public static LogHelper Default { get; private set; }

        static LogHelper()
        {
            Default = new LogHelper(ExceptionlessClient.Default);
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="tags"></param>
        public void WriteExceptionLog(Exception ex,string message="", params string[] tags)
        {
            var msg = ex.Message;
            if (!string.IsNullOrWhiteSpace(message)) msg = message;
            this.logger.CreateException(ex).SetMessage(msg).AddTags(tags).Submit();
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="tags"></param>
        public void WriteExceptionLog(Exception ex, params string[] tags)
        {
            this.logger.CreateException(ex).SetMessage(ex.Message).AddTags(tags).Submit();
        }


        /// <summary>
        /// 写日志 
        /// </summary>
        public void WriteLog(string message,LogLevel logLevel, params string[] tags)
        {
            switch (logLevel)
            {
                case LogLevel.Warning:
                    this.logger.CreateLog(message, Exceptionless.Logging.LogLevel.Warn).AddTags(tags).Submit();
                    break;
                case LogLevel.Error:
                    this.logger.CreateLog(message, Exceptionless.Logging.LogLevel.Error).AddTags(tags).Submit();
                    break;
                case LogLevel.Trace:
                    this.logger.CreateLog(message, Exceptionless.Logging.LogLevel.Trace).AddTags(tags).Submit();
                    break;
                case LogLevel.Debug:
                    this.logger.CreateLog(message, Exceptionless.Logging.LogLevel.Debug).AddTags(tags).Submit();
                    break;
                default:
                    this.logger.CreateLog(message, Exceptionless.Logging.LogLevel.Info).AddTags(tags).Submit();
                    break;
            }
           
        }

        /// <summary>
        /// 写日志 Info
        /// </summary>
        /// <param name="message"></param>
        /// <param name="tags"></param>
        public void WriteLog(string message, params string[] tags)
        {
            this.logger.CreateLog(message, Exceptionless.Logging.LogLevel.Info).AddTags(tags).Submit();
        }

    }
}